

package traffic.light.simulator.town.trafficLight;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Date;
import traffic.light.simulator.town.unity.CrossRoadType;
import traffic.light.simulator.town.unity.TownElement;
import traffic.light.simulator.town.unity.TownUnity;

/**
 *
 *Class of Trafficlight which change with pedestrian and every 3 seconds
 */
public class HybridTrafficLight extends TrafficLight implements IPedestrianTrafficLight,ITimesTrafficLight {

    private Date time;
    private long difference=0;
    private boolean continu;
    
    public HybridTrafficLight(int xTU, int yTU,CrossRoadType type) {
        super(xTU, yTU,type);
    }

    @Override
    public void run() {
        this.time = new Date();
        this.continu=true;
        while(true){
            if(this.continu && this.time.getTime()+3000<=new Date().getTime()){
                this.change();
                this.time=new Date();
            }
            else{
                Thread.yield();
            }
        }
    }

    @Override
    public void CallFromPedestrian() {
        this.time=new Date(new Date().getTime());
        this.change();
    }

    @Override
    public void setTime(boolean continu) {
        this.continu=continu;
         if(continu){
            this.time=new Date(new Date().getTime()-this.difference);
        }
        else{
            this.difference=new Date().getTime()-this.time.getTime();
            this.time=new Date(this.time.getTime()-4000);
        }
    }

    @Override
    public void draw(Graphics g) {
        int x=xTU*TownUnity.getSize().width;
        int y=yTU*TownUnity.getSize().height;
        g.setColor(Color.white);

        g.drawLine(x+2*TownElement.getSize(),y+2*TownElement.getSize(),x+4*TownElement.getSize()-1,y+4*TownElement.getSize()-1);
        g.drawLine(x+3*TownElement.getSize(),y+2*TownElement.getSize(),x+4*TownElement.getSize()-1,y+3*TownElement.getSize());
        g.drawLine(x+2*TownElement.getSize(),y+3*TownElement.getSize(),x+3*TownElement.getSize(),y+4*TownElement.getSize()-1);

        g.drawLine(x+4*TownElement.getSize()-1,y+2*TownElement.getSize(),x+2*TownElement.getSize(),y+4*TownElement.getSize()-1);
        g.drawLine(x+3*TownElement.getSize(),y+2*TownElement.getSize(),x+2*TownElement.getSize(),y+3*TownElement.getSize());
        g.drawLine(x+4*TownElement.getSize()-1,y+3*TownElement.getSize(),x+3*TownElement.getSize(),y+4*TownElement.getSize()-1);

        super.draw(g);
    }

    @Override
    public void draw(Graphics g, boolean editionMode) {
        this.draw(g);
    }



}
